//
//  HDAboutUsViewController.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/7.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

class HDAboutUsViewController: UIViewController {
    let tableView = UITableView(frame: CGRect.zero, style: .grouped)
    let iconImageView = UIImageView(image: UIImage(named: "组-2"))
    let nameLabel = UILabel()
    let xxLabel = UILabel()
    
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 260))
    let lineView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: 0xfcfcfc)
        tableView.backgroundColor = UIColor(hex: 0xfcfcfc)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.backgroundColor = .clear
//        if tableView.respondsToSelector(Selector("setSeparatorInset:")) {
//            tableView.separatorInset = UIEdgeInsetsZero
//        }
//        if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
//            tableView.layoutMargins = UIEdgeInsetsZero
//        }
        setupUI()
        setupAutoLayout()
    }
    
    
    fileprivate func setupUI() {
        title = "关于我们"
        
        tableView.config { (set: UITableView) -> Void in
            set.backgroundColor = .clear
            set.tableHeaderView = self.headerView
            set.tableFooterView = UIView()
            set.isScrollEnabled = false
        }
        iconImageView.contentMode = .scaleAspectFit
        iconImageView.backgroundColor = UIColor(hex:0xfcfcfc)
        nameLabel.config { (set: UILabel) -> Void in
            set.text = AppConstant.appName + "V" + AppConstant.appVersion
            set.font = .systemFont(ofSize: 14)
            set.textColor = .black
        }
        xxLabel.config { (set: UILabel) -> Void in
            set.text = "用速度与透明定义理赔"
            set.font = .systemFont(ofSize: 14)
            set.textColor = .lightGray
            
        }
        lineView.backgroundColor = UIColor.white
        headerView.addSubview(iconImageView)
        headerView.addSubview(nameLabel)
        headerView.addSubview(lineView)
        headerView.addSubview(xxLabel)
        
        view.addSubview(tableView)
        
        
    }
    
    
    fileprivate func setupAutoLayout() {
        tableView.snp.makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        iconImageView.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view.snp.top).offset(50)
            make.size.equalTo(100)
            
        }
        nameLabel.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconImageView.snp.bottom).offset(10)
            
        }
        xxLabel.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(nameLabel.snp.bottom).offset(10)
        }
        lineView.snp.makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: AppConstant.screenWidth, height: 1))
            make.bottom.equalTo(headerView.snp.bottom)
            make.left.equalTo(headerView.snp.left)
        }
        
    }
    
}

extension HDAboutUsViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "用户协议"
        cell.backgroundColor = UIColor.white
        cell.accessoryType = .disclosureIndicator
        cell.selectionStyle = .none
        return cell
    }
    
    func onUserAgreement () {
        navigationController?.pushViewController(HDUserAgreementViewController(), animated: true)
    }
}

extension HDAboutUsViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.perform(#selector(HDAboutUsViewController.onUserAgreement))
        tableView.deselectRow(at: indexPath, animated: true)
    }
//    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
//        if cell.respondsToSelector(Selector("setSeparatorInset:")) {
//            cell.separatorInset = UIEdgeInsetsZero
//        }
//        if cell.respondsToSelector(Selector("setLayoutMargins:")) {
//            cell.layoutMargins = UIEdgeInsetsZero
//        }
//    }
}

